package com.googlecode.synccontacts.listeners;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import com.googlecode.synccontacts.activity.MainActivity;
import com.googlecode.synccontacts.activity.SettingActivity;
import com.googlecode.synccontacts.dao.DAOUtil;
import com.googlecode.synccontacts.dao.entity.Account;

/**
 * User: volkovma@inbox.ru
 * Date: 11.02.11
 * Save accounts listener
 */
public class SaveAccountListener implements View.OnClickListener {
    private int netType;
    private EditText netLogin;
    private EditText netPass;
    private Activity parentActivity;


    /**
     * Конструктор
     *
     * @param SocNetType для какой сети открыть форму
     */
    public SaveAccountListener(int SocNetType, Activity parentActivity,EditText netLogin,EditText netPass) {
        this.netType = SocNetType;
        this.netLogin = netLogin;
        this.netPass = netPass;
        this.parentActivity = parentActivity;
    }

    /**
     * Обработчик нажатия
     * @param view
     */
    public void onClick(View view) {
        String netLoginText = netLogin.getText().toString();
        String netPassText = netPass.getText().toString();
        if(netLoginText ==null||"".equals(netLoginText)){
            parentActivity.showDialog(SettingActivity.DIALOG_LOGGIN_ID);
            return;
        }
        if(netPassText ==null||"".equals(netPassText)){
            parentActivity.showDialog(SettingActivity.DIALOG_PASS_ID);
            return;
        }
        DAOUtil daoUtil = new DAOUtil(parentActivity);
        Account acc =  new Account();
        acc.setSocialNetworkType(netType);
        acc.setLogin(netLoginText);
        acc.setPass(netPassText);
        long res = daoUtil.saveAccount(acc);

        if(res<0){
            parentActivity.showDialog(SettingActivity.DIALOG_ERROR_ID);
            return;
        }
        Intent intent = new Intent();
        intent.setClass(parentActivity, MainActivity.class);
        parentActivity.startActivity(intent);
        parentActivity.finish();
    }
}
